<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Vote\ExportVoteSubject;
use App\Models\Vote\VoteSubject;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
 * Class FrontendController
 * @package App\Http\Controllers
 */
class FrontendController extends Controller
{
//{
// "id":"47985990",
// "txt":"\u5973\u5b69\u5b50\u9009\u5e02\u573a\u90e8\u6bd4\u8f83\u597d\u5417\uff1f",
// "views":1,
// "likes":0,
// "created_at":"1461423404",
// "channel":
//      {
//          "id":"749", "name":"\u6295\u7968\u6839\u672c\u505c\u4e0d\u4e0b\u6765", "icon":"http:\/\/s.same.com\/1000\/1aa3f3fcdab9f72067f318f52903f16d.png", "cate":"6", "times":144574, "config":
//          {
//          "tabs":
//          {
//          "default_tab":0, "content":[{"title":"\u6700\u65b0", "url":"{default_url}", "style":"senses"},{"title":"\u9891\u9053\u7b80\u4ecb","style":"miscinfo"}]
//          }
//          },
//          "user_id":"73"
//      },
//"user":{
//    "id":"5096901",
//      "is_active":"1",
//      "username":"\u02caLiu \u2022",
//      "avatar":"http:\/\/s.same.com\/image\/7ab5eaadc8c45f7a66c896f3648414d1_w749_h752.jpg",
//      "created_at":"1453637323",
//      "is_staff":"0",
//      "timezone":"Asia\/Shanghai",
//      "latest":"[\u6295\u7968\u6839\u672c\u505c\u4e0d\u4e0b\u6765] \u5973\u5b69\u5b50\u9009\u5e02\u573a\u90e8\u6bd4\u8f83\u597d\u5417\uff1f",
//      "config":{"push":0,"voice":1,"shock":0},
//      "meta":{"firstblood":1453637621}
//  },
//"media":{
//    "vote":{"id":"357679",
//          "choices":[{"title":"a","order":1,"votes":0},{"title":"b","order":2,"votes":0}],
//          "total":0,
//          "user_id":"5096901",
//          "title":"\u5973\u5b69\u5b50\u9009\u5e02\u573a\u90e8\u6bd4\u8f83\u597d\u5417\uff1f",
//          "sense_id":"47985990",
//          "my_choice":"0"
//          }
//    },
//"is_liked":"0"}

    /**
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $page = $request->query("page");
        if ($page == null) {
            $page = 1;
        }
        //使用查询构建器进行简单分页，每页显示10条记录
        $votes = ExportVoteSubject::select(
            DB::Raw('*,total/views as voterate')
        )->orderBy("voterate", "desc")->paginate(1);
        $next_page = $votes->hasMorePages() ? $page + 1 : -1;
        return view('frontend.index', ['votes' => $votes, 'next_page' => $next_page]);
    }

    /**
     * @return \Illuminate\View\View
     */
    public function nextSubject(Request $request)
    {
        $page = $request->query("page");
        if ($page == null) {
            $page = 1;
        }
        //使用查询构建器进行简单分页，每页显示10条记录
        $votes = ExportVoteSubject::select(
            DB::Raw('*,total/views as voterate')
        )->orderBy("voterate", "desc")->paginate(1);
        $votes->get(0)->page = $page;
        return response()->json($votes->get(0));
    }

    /**
     * 转换爬到的数据
     * @return \Illuminate\View\View
     */
    public function transformSpiderData()
    {
//        $jsonFiles = Storage::disk('local')->allFiles('same_vote_data');
//        $i = 0;
        $data = array();
//        foreach ($jsonFiles as $jsonFile) {
//            $i++;
//            $voteDataBundleTxt = Storage::disk('local')->get($jsonFile);
//            $voteDataBundleObj = json_decode($voteDataBundleTxt); //将json字符串反序列化为对象数组
//            $voteSubjectWrappers = $voteDataBundleObj->data->results;
//            for ($j = 0; $j < sizeof($voteSubjectWrappers); $j++) {
//                $voteSubjectWrapper = $voteSubjectWrappers[$j];
//                $voteSubjectJson = $voteSubjectWrapper->media->vote;
//                $voteSubjectModel = new VoteSubject;
//                $voteSubjectModel->views = $voteSubjectWrapper->views;
//                $voteSubjectModel->likes = $voteSubjectWrapper->likes;
//                $voteSubjectModel->created_at = $voteSubjectWrapper->created_at;
//                $voteSubjectModel->id = $voteSubjectJson->id;
//                $voteSubjectModel->total = $voteSubjectJson->total;
//                $voteSubjectModel->user_id = $voteSubjectJson->user_id;
//                $voteSubjectModel->title = $voteSubjectJson->title;
//                $voteSubjectModel->sense_id = $voteSubjectJson->sense_id;
//                $voteSubjectModel->my_choice = $voteSubjectJson->my_choice;
//                $voteSubjectModel->choices = $voteSubjectJson->choices;
//                $voteSubjectModel->save();
////                $data['test'.$i.$j] = json_encode($voteSubjectModel);
//            }
//        }
        $data['test11'] = "ok";
        return view('frontend.index', $data);
    }

    /**
     * @return \Illuminate\View\View
     */
    public function macros()
    {
        return view('frontend.macros');
    }
}
